import {ajax_basic as ajax, ajax_user} from './ajax';
import { setItem, setItemSimple, removeItem, removeItemSimple } from './storage';
import storage from '../config/storage.json';

import { login as netease_login, logout as netease_logout } from './netease';

import {
    ToastAndroid,
    DeviceEventEmitter
  } from 'react-native';

export const SETUSERINFO = "myuser_setuserinfo";

//登录
export function login(event, phone, passport){
  ajax("passport/phone_login", { phone: phone, password: passport })
  .then(responseJson => {
    if (responseJson.result != 1) {
      ToastAndroid.showWithGravity(responseJson.msg, ToastAndroid.SHORT, ToastAndroid.CENTER);
    } else {
      let diapatch = event.props.dispatch;
      let navigation = event.props.navigation;
      _save_info(navigation, diapatch, responseJson);
      navigation.navigate('Mainpage');
    }
  })
  .catch(err => {
    ToastAndroid.show("网络连接失败", ToastAndroid.SHORT);
  })
}

//登出
export function logout(){
  global.uid = "";
  global.token = "";
  removeItemSimple("token");
  DeviceEventEmitter.removeListener('onMessage');
  netease_logout();
}

//微信登陆
export function login_wechat(event, code){
    ajax("passport/login_wechat", { code: code })
    .then(responseJson => {
      if (responseJson.result != 1) {
        ToastAndroid.showWithGravity(responseJson.msg, ToastAndroid.SHORT, ToastAndroid.CENTER);
      }else{
        let diapatch = event.props.dispatch;
        let navigation = event.props.navigation;
        if (responseJson.data.type == 'reg') {
          _save_info(navigation, diapatch, responseJson);
          event.props.navigation.navigate('RegFillInfo');
        } else if (responseJson.data.type == 'login') {
          _save_info(navigation, diapatch, responseJson);
          navigation.navigate('Mainpage');
        }
      }
    })
    .catch(err => {
      ToastAndroid.show("网络连接失败", ToastAndroid.SHORT);
    })
}

function _save_info(navigation, dispatch, responseJson){
  let uid = responseJson.data.uid;
  let token = responseJson.data.token;
  global.uid = uid;
  global.token = token
  let netease_token = responseJson.data.netease_token;
  netease_login(uid, netease_token, function(){
    logout();
    navigation.navigate('Reglogin', {transition: 'pop'});
  });
  setItem(storage.userinfo.nickname, responseJson.data.nickname);
  setItem(storage.userinfo.avatar, responseJson.data.appface);
  setItem(storage.userinfo.sex, responseJson.data.sex);
  setItemSimple("account", uid);
  setItemSimple("token", token);
  dispatch({
    type: SETUSERINFO,
    uid,
    token,
    avatar: responseJson.data.appface,
    nick: responseJson.data.nickname,
    sex: responseJson.data.sex
  });
}

//填写信息
export function fill_info(event, nickname, sex, birthday){
  ajax_user("passport/update", { nickname: nickname, sex: sex, birthday: birthday })
  .then(responseJson => {
    if (responseJson.result != 1) {
      ToastAndroid.show(responseJson.msg, ToastAndroid.SHORT);
    } else{
      setItem(storage.userinfo.sex, sex);
      event.props.navigation.navigate('RegFillFavor');
    }
  })
  .catch(err => {
    ToastAndroid.show("网络连接失败", ToastAndroid.SHORT);
  })
}